package cn.jinjimi.app.controller;

import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;

import java.util.concurrent.Callable;

import bolts.Continuation;
import bolts.Task;

/**
 *
 * @author zhenhua
 * @date 2018/2/4
 */

public class PayController {

    public static Task<Void> alipay(final PayTask task, final String tradeInfo) {
        return Task.callInBackground(new Callable<H5PayResultModel>() {
            @Override
            public H5PayResultModel call() throws Exception {
                return  task.h5Pay(tradeInfo, true);
            }
        }).continueWith(new Continuation<H5PayResultModel, Void>() {
            @Override
            public Void then(Task<H5PayResultModel> task) throws Exception {
                H5PayResultModel result = task.getResult();
                //9000——订单支付成功
                //8000——正在处理中
                //4000——订单支付失败
                //5000——重复请求
                //6001——用户中途取消
                //6002——网络连接出错
                if (result != null) {
                    String resultCode = result.getResultCode();
                    if ("9000".equals(resultCode)) {
                        //onResult(new PayResult(PayResult.CODE_SUCCESS, "支付成功", XrkAlipay.TYPE, result.getReturnUrl()));
                    } else if ("4000".equals(resultCode) || "6002".equals(resultCode)) {
                        //onResult(new PayResult(PayResult.CODE_ERROR, "支付失败", XrkAlipay.TYPE, result.getReturnUrl()));
                    } else if ("6001".equals(resultCode)) {
                        //onResult(new PayResult(PayResult.CODE_CANCEL, "取消支付", XrkAlipay.TYPE, result.getReturnUrl()));
                    }
                }
                return null;
            }
        });
    }
}
